package com.example.freemarker.controller;

import com.example.freemarker.pojo.User;
import org.springframework.core.Conventions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class FreemarkerController {

    @GetMapping("/getFirstFreemarker")
    public ModelAndView getFirstFreemarker(Model model) {

        ModelAndView modelAndView = new ModelAndView();

        ModelMap modelMap = new ModelMap();

        List<User> userList = new ArrayList<>();

        userList.add(new User(1, "罗", "a1!"));

        userList.add(new User(12, "高", "123"));


        modelMap.addAttribute(userList);
        /**
         * 相当于
         * modelMap.addAttribute("userList",userList);
         */

        System.out.println("当传入的key-value只有value时，key用变量名" + Conventions.getVariableName(userList) + "充当");


        modelAndView.addAllObjects(modelMap);

        modelAndView.setViewName("/userList");

        return modelAndView;
    }
}
